import stripe
from rest_framework.exceptions import ValidationError


def create_stripe_id(provider, data):
    try:
        token = stripe.Token.create(
            card={
                "number": data.get('card_number'),
                "exp_month": data.get('exp_month'),
                "exp_year": data.get('exp_year'),
                "cvc": data.get('card_cvc'),
                "address_zip": data.get('zip_code')
            }
        )
        email = provider.email
        stripe_account = stripe.Customer.create(
            email=email
        )
        new_source = stripe.Customer.create_source(
            stripe_account.id,
            source=token,
        )

        default_payment = data.get('default_payment', False)
        if default_payment is True:
            stripe.Customer.modify(
                stripe_account.id,
                default_source=new_source.id
            )

        return stripe_account.id
    except:
        raise ValidationError('Card is invalid')